package fr.enseirb.mycub.component.map.parking;

import java.util.ArrayList;
import java.util.Hashtable;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.util.Log;
import android.widget.FrameLayout;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import fr.enseirb.mycub.R;
import fr.enseirb.mycub.component.map.OverlayBundle;
import fr.enseirb.mycub.component.map.OverlayBundleItem;
import fr.enseirb.mycub.structures.Line;
import fr.enseirb.mycub.structures.Parking;
import fr.enseirb.mycub.structures.SpecificLine;
import fr.enseirb.mycub.structures.bus.BusStation;

public class ParkingsOverlay extends OverlayBundle {

	private ArrayList<? extends OverlayBundleItem> items = new ArrayList<ParkingOverlay>();
	static ClassicDrawable standard = null;
	static PinDrawable pin = null;
	static Paint mTextPaint = new Paint();
	static Paint mPinPaint = new Paint();
	static boolean mDrawText;

	int IMAGE_HEIGHT;

	public ParkingsOverlay(Resources resources, BitmapDrawable parkingPin, MapView mv, Context context) {
		//this(resources,drawable1,mv,context);
		super(parkingPin, mv, context);

		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(19);
		mTextPaint.setTextAlign(Align.RIGHT);
		mTextPaint.setColor(Color.BLACK);
		mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

		mPinPaint.setStyle(Paint.Style.FILL);
		mPinPaint.setAlpha(150);
		mPinPaint.setAntiAlias(true);
		
		ParkingsOverlay.standard = new ClassicDrawable(resources, parkingPin.getBitmap());
		
		boundCenterBottom(standard);
		pin = new PinDrawable();
		ClassicDrawable.mPaint.setColor(Color.WHITE);
		IMAGE_HEIGHT = standard.getIntrinsicHeight();
		
		populate();
	}



	@Override
	public void setItems(ArrayList<? extends OverlayBundleItem> list) {
		items = list;
		setLastFocusedIndex(-1);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return (items.get(i));
	}

	public ArrayList<? extends OverlayBundleItem> getOverlayList() {
		return items;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			if (mapView.getZoomLevel() >= 16) {
				mDrawText = true;
				setBalloonBottomOffset(IMAGE_HEIGHT);
			} else {
				mDrawText = false;
				setBalloonBottomOffset(0);
			}
			super.draw(canvas, mapView, shadow);
		}
	}

	@Override
	public int size() {
		return (items.size());
	}

	public ArrayList<? extends OverlayBundleItem> getOverlaysFromCursor(
			ArrayList<Cursor> itemsCursor, Location location, int distanceFilter) {
		
		Cursor parkingCursor = itemsCursor.get(0);
		ArrayList<Parking> stations = Parking.getParkingsFromCursor(parkingCursor);

		ArrayList<OverlayBundleItem> overlays = new ArrayList<OverlayBundleItem>(
				parkingCursor.getCount());
		ParkingOverlay overlay;
		Location stationLocation = new Location("");
		float distance = 0;
		for(int i = 0; i < stations.size(); i++) {
		 	final Parking parking = stations.get(i);
			overlay = new ParkingOverlay(
					parking.getGeoPoint(),
					parking.getId());
			overlay.setPlaces(parking.getParkingPlaces());
			Log.i("PARKINGS ", parking.getName()+" places : "+parking.getParkingPlaces());
			if (distanceFilter != 0 && location != null) {
				stationLocation.setLatitude(((double) parking.getGeoPoint().getLatitudeE6()) * 1E-6);
				stationLocation.setLongitude(((double) parking.getGeoPoint().getLongitudeE6()) * 1E-6);
				distance = location.distanceTo(stationLocation);
				if (distance > distanceFilter) {
					continue;
				}
			}
			overlays.add(overlay);
		}
		return overlays;
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		return true;
	}



	@Override
	protected FrameLayout createBalloonOverlayView() {
		BalloonOverlayViewStationsParking<OverlayItem> b =  new BalloonOverlayViewStationsParking<OverlayItem>(getMapView().getContext(),
				viewOffset);
		return b;
	}




}